package com.fancy.learncenter.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.MyCollectionDataBean;
import com.fancy.learncenter.bean.MyMsgDataBean;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.common.LoginUserSharePrefernce;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.ui.adapter.base.CommonRecycleViewAdapter;
import com.fancy.learncenter.ui.adapter.base.CustomViewHold;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.utils.SPUtils;
import com.fancy.learncenter.utils.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Response;

import static com.fancy.learncenter.utils.NetUtil.getResResult;

public class MyMsgActivity extends BaseActivity {

    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;


    MyMsgAdapter msgAdapter;
    ArrayList<MyMsgDataBean.MessageVOListBean> listData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_msg, Status.STATUS, "消息通知");
        ButterKnife.bind(this);

        initRecycleView();

        getListData();
    }

    private void initRecycleView() {
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);

        listData = new ArrayList<>();
        msgAdapter = new MyMsgAdapter(this, listData);
        recyclerView.setAdapter(msgAdapter);
    }

    private void getListData() {
        HttpMehtod.getInstance().getAllMessage(new CustomNetSubscriber(this) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {
                //解析数据
                BaseBean<MyMsgDataBean> baseBean = Utils.fromJsonObject(getResResult(responseBodyResponse), MyMsgDataBean.class);
                //数据业务逻辑处理
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<MyMsgDataBean>() {
                    @Override
                    public void success(MyMsgDataBean dataBean) {
                        msgAdapter.notifyDataSetChanged(dataBean.getMessageVOList());
                    }

                    @Override
                    public void fail(String errorCode) {

                    }


                }).dealData();
            }

            @Override
            public void onCompleted() {
                super.onCompleted();
            }
        });
    }

    class MyMsgAdapter extends CommonRecycleViewAdapter<MyMsgDataBean.MessageVOListBean> {

        public MyMsgAdapter(Context mContext, List<MyMsgDataBean.MessageVOListBean> itemDatas) {
            super(mContext, R.layout.activity_my_msg_item, itemDatas);
        }

        @Override
        public void bindView(CustomViewHold customViewHold, final MyMsgDataBean.MessageVOListBean item, int position) {
            TextView see = customViewHold.getView(R.id.see);
            TextView title = customViewHold.getView(R.id.title);
            title.setText(item.getMessageTitle());
            TextView time = customViewHold.getView(R.id.time);
            time.setText(item.getCreateTimeFormat());
            TextView content = customViewHold.getView(R.id.content);
            content.setText(item.getMessageInfo());


            if (item.getState() == 0) {
                title.setTextColor(getResources().getColor(R.color.red));
                see.setTextColor(getResources().getColor(R.color.red));
            } else {
                see.setTextColor(getResources().getColor(R.color.black));
                title.setTextColor(getResources().getColor(R.color.black));
            }

            customViewHold.getRoorView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MyMsgActivity.this, MyMsgDetailActivity.class);
                    intent.putExtra("title", item.getMessageTitle());
                    intent.putExtra("time", item.getCreateTimeFormat());
                    intent.putExtra("content", item.getMessageInfo());
                    intent.putExtra("messageId", item.getId());
                    startActivityForResult(intent, Constant.FRESH_CALLBACK);
                }
            });
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == Constant.FRESH_CALLBACK && resultCode == Constant.FRESH_RESULT_CALLBACK) {
            if (LoginUserSharePrefernce.isLogin()) {
                getListData();
            }

        }
    }
}
